// 表单的验证信息
function Validate(){}
$.extend(Validate.prototype,{
    init:function(){
        this.inputs = $(".resister_item input");
        this.r_btn  = $("#resister");
        this.explain =$(".explain input");
        this.error  =$(".error");
        this.error_txt = $(".error_txt");
        this.dis     = this.disable()
        //隐私条款必须要选中
        this.explain.on("click",$.proxy(function(){
              this.dis = this.disable()
        },this))
        // 失焦 进行验证合法性 并作出标记
        this.inputs.on("blur",$.proxy(function(){
            var  regs = this.regular()
            this.validate(regs);
        },this))
        // 提交的时候进行 提示
        this.r_btn.on("click",$.proxy(function(){
           this.falg = this.resister()  
            // 提交返回true的话  进行localStorage的创建
            if(this.falg){
                this.set();
            }
        },this))
    },
    regular:function(evt){
        var e = evt || window.event;
        var target = e.target || e.srcElement;
        var opsition = $(target).attr("class")
        var reg = null;
        var msg = "";
        switch(opsition){
            case "user" : 
                reg = /^[a-zA-Z][a-zA-Z0-9]{3,15}$/;
                msg = "用户名输入不正确";
            break;
            case "phones" : 
                reg = /^1[3456789]\d{9}$/;
                msg = "手机号输如有误";    
            break;
            case "pwd"    : 
                reg = /^[a-zA-Z0-9\.\+@#$%\*=]{8,20}/;
                msg = "密码格式不正确"
            break;
            case  "pwds"   :;
            case "phoness" : 
                reg = 1;
                msg = "";
            break;
            case "repwd" : 
                reg = null;
                msg = "密码不一致"
            break;
        }
        return {
            reg : reg ,
            ele : target,
            msg : msg
        } ;
    },
    validate:function(regs){
        var reg  = regs.reg;
        var ele  = $(regs.ele);
        var msg  = regs.msg;
        var text = ele.val();
        if(reg == 1){
            return false;
        }
        if(!reg){
            if(text == $(".pwd").val()){
                ele.addClass("trues")
            }else{
                ele.addClass("falses")
                ele.attr("msg",msg)
                return false;
            }
        }else{
            if( reg.test(text) ){
                ele.addClass("trues")
            }else{
                ele.addClass("falses")
                ele.attr("msg",msg)
                return false;
            }
        }
    },
    disable:function(){
       var dis = this.explain.is(":checked") ;
       return dis ;
    },
    resister : function(){
       this.error.css("display","none")
       var msgs = $(".falses");
       var falg = "";
       if(!this.dis){
           alert("请仔细阅读联想使用条款和隐私权政策！");
           return false;
       }
       if(msgs[0]){
            this.error.css("display","block")
            this.error_txt.html(msgs.attr("msg"));
           $(msgs[0]).removeClass("falses")
           return false
       }
       for(var i = 0,item ; item = $(".r")[i];i++){
           if($(item).children().val() == ""){
                this.error.css("display","block")
                this.error_txt.html("请填写完整信息");
                return false 
           }
       }
       return true;
    },
    set:function(){
        var list = localStorage.getItem("user");
        if(list){
            var falg = false;
            list = JSON.parse(list);
            for(var i = 0,item ; item = list[i];i++){
                 if(item.user == $(".user").val()){
                     console.log(item.user, $(".user").val());
                     falg = true;
                     alert ("用户名已存在!");
                     return false;
                 }
            }
            if(!falg){
                list.push({
                    user : $(".user").val(),
                    phone: $(".phones").val(),
                    password: $(".pwd ").val(),
                    status  : true
                 })
            }
            localStorage.setItem("user",JSON.stringify(list));
        }else{
            var lists = [{
                user : $(".user").val(),
                phone: $(".phones").val(),
                password: $(".pwd ").val(),
                status  : true
            }]
            localStorage.setItem("user",JSON.stringify(lists));
        }
        location.href = "../html/index.html";
    }
})
export default new Validate();